import requests
import io
import os
import urllib.request
from lxml import etree 

url = ''

def filename_html(url):
    url_before = url
    url_after = url_before.split("/")
    return url_after[3] + "_" + url_after[5] + ".html"

def filename_m3u8(url):
    url_before = url
    url_after = url_before.split("/")
    return url_after[3] + "_" + url_after[5] + ".m3u8"

def filename_txt(url):
    url_before = url
    url_after = url_before.split("/")
    return url_after[3] + "_" + url_after[5] + ".txt"

def download_html(url):
    response = urllib.request.urlopen(url)
    string = response.read()
    html = string.decode('utf-8')
    if not os.path.exists('temp/'):
        os.mkdir('temp/')
    f = open('temp/' + filename_html(url),'w',encoding='utf-8')
    f.write(html)
    f.close()

def m3u8_url(url):
    html = etree.parse('temp/' + filename_html(url),etree.HTMLParser())
    video_tag = html.xpath('//video/@data-movie-playlist')
    video_tag_aftercut = video_tag[0].split("\"")
    return video_tag_aftercut[9]